import { Injectable } from '@angular/core';
import { MenuParamDict } from "../entity/menu-param-dict.model";
import { Http } from "@angular/http";
import { MenuParamService } from "./menu_param.service";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class MenuParamDictService {
  public menuParamDict: MenuParamDict = {};
  private menuParamService: MenuParamService;
  constructor(
    private http: InterceptorService,
  ) {
  }

  /**
   * 初始化数据
   * 
   * @returns {Promise<boolean>} 
   * @memberof MenuParamDictService
   */
  public initData(): Promise<boolean> {
    this.menuParamService = new MenuParamService(this.http);
    return this.menuParamService.getMenuParams()
      .then(res => {
        res.forEach(menuParam => {
          if (this.menuParamDict[menuParam.menuId]) {
            
            //if (!this.menuParamDict[menuParam.menuId][menuParam.paramName]) {
              
              this.menuParamDict[menuParam.menuId][menuParam.paramName] = menuParam;
            //}
          }
          else {
            this.menuParamDict[menuParam.menuId] = {};
            this.menuParamDict[menuParam.menuId][menuParam.paramName] = menuParam;
          }
        });
        return Promise.resolve(true);
      })
      .catch(error => {
        throw error;
      });
  }

}


// WEBPACK FOOTER //
// ./src/app/service/menu-param-dict.service.ts